package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController("adminCategory")
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    //新增分类
    @PostMapping
    @ApiOperation("新增分类")
    public Result insert(@RequestBody Category category){
        log.info("新增的玩意:{}",category);
        categoryService.insert(category);

        return Result.success();
    }
    //分类分页查询
    @GetMapping("/page")
    @ApiOperation("菜品分类分页")
    public Result CategoryPage(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult page = categoryService.CategoryPage(categoryPageQueryDTO);

        return page != null ? Result.success(page) : Result.error("查询失败");
    }
    //根据id删除分类
    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result daleteById(Long id){
        categoryService.daleteById(id);

        return Result.success();
    }
    //修改启用禁用
    @PostMapping("/status/{status}")
    @ApiOperation("修改启用禁用")
    public Result startAndStop(@PathVariable int status, int id){
        categoryService.startAndStop(status,id);

        return Result.success();
    }
    //修改分类
    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody Category Category){
        boolean result = categoryService.updateCategory(Category);

        return result ?  Result.success("修改成功") : Result.error("修改失败");
    }
    //根据类型查询分类
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result findByType(Integer type){
        List<Category> list = categoryService.findByType(type);
        return Result.success(list);
    }

}
